 var datepickerDateFormat = 'yy-mm-dd';
 var displayDateFormat = 'yy-mm-dd';

(function($) {

	var jqValidatorDefaultOnKeyUp = jQuery.validator.defaults.onkeyup;

	$.validator
			.setDefaults({
				errorElement : 'span',
				errorClass: 'valid-error',

				onkeyup : function(element) {
					var elementName = jQuery(element).attr('name');
					if (this.settings.rules[elementName] != undefined) {

						var applyDefaultFunction = true;
						if ((this.settings.rules[elementName].onkeyup === false)
								|| ((this.settings.rules[elementName].onkeyup === 'if_invalid') && (this.invalid[element.name] === undefined))) {
							applyDefaultFunction = false;
						}

						if (applyDefaultFunction) {
							jqValidatorDefaultOnKeyUp.apply(this, arguments);
						}
					}
				}
			});
	
	$.validator.addMethod("validToYYYYMM", function(value, element) {
		var str = value;
		var valid = true;
		var reg = /^[1-9]{1}[0-9]{3}-((0[1-9]{1})|(1[0-2]{1}))$/;     
        var r = str.match(reg);     
        if(r==null){    
        	valid = false;
        }
        return valid;
	});
	
	$.validator.addMethod('date_range', function(value, element, params) {
	var valid = false;
	if (params.fromDate != undefined) {
		var fromDate = $.trim(params.fromDate);
		var toDate = $.trim(value);
	} else {
		var fromDate = $.trim(value);
		var toDate = $.trim(params.toDate);
	}
	var format = params.format;
	var displayFormat = '';
	if (params.displayFormat != undefined) {
		displayFormat = params.displayFormat;
	} else {
		displayFormat = format;
	}
	if (fromDate == displayFormat || toDate == displayFormat || fromDate == ""
			|| toDate == "") {
		valid = true;
	} else {
		var parsedFromDate = $.datepicker.parseDate(format, fromDate);
		var parsedToDate = $.datepicker.parseDate(format, toDate);
		if (parsedFromDate <= parsedToDate) {
			valid = true;
		}
	}
	return valid;
});

}(jQuery));

/*jQuery.validator.addMethod("isPhone", function(value,element) {
	  var length = value.length;
	  var mobile = /^(((13[0-9]{1})|(15[0-9]{1}))+\d{8})$/;
	  var tel = /^\d{3,4}-?\d{7,9}$/;
	  return this.optional(element) || (tel.test(value) || mobile.test(value));
	}); */


jQuery.validator.addMethod("isPhone", function(value,element) {
	  var length = value.length;
	  var mobile = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/; 
	  return this.optional(element) || (length ==11 && mobile.test(value));
	}); 

//电话号码验证
jQuery.validator.addMethod("telPhone", function(value, element) 
{
var tel = /^(\d{3,4}-?)?\d{7,9}$/g;
return this.optional(element) ||(tel.test(value));
});

jQuery.extend(jQuery.validator.messages, {
	  validToYYYYMM : "请输入正确格式YYYY-MM",
	  required: "必选字段",
	  date_range: "结束日期必须大于开始日期",
	  remote: "请修正该字段",
	  email: "请输入正确格式的电子邮件",
	  url: "请输入合法的网址",
	  date: "请输入合法的日期",
	  dateISO: "请输入合法的日期 (ISO).",
	  number: "请输入合法的数字",
	  digits: "只能输入整数",
	  creditcard: "请输入合法的信用卡号",
	  equalTo: "请再次输入相同的值",
	  accept: "请输入拥有合法后缀名的字符串",
	  maxlength: jQuery.validator.format("请输入一个 长度最多是 {0} 的字符串"),
	  minlength: jQuery.validator.format("请输入一个 长度最少是 {0} 的字符串"),
	  rangelength: jQuery.validator.format("请输入 一个长度介于 {0} 和 {1} 之间的字符串"),
	  range: jQuery.validator.format("请输入一个介于 {0} 和 {1} 之间的值"),
	  max: jQuery.validator.format("请输入一个最大为{0} 的值"),
	  min: jQuery.validator.format("请输入一个最小为{0} 的值"),
	  isPhone: "请正确填写您的联系电话",
      telPhone: "请正确填写您的座机号码"
	});